package com.leyou.goods.listener;

import com.leyou.goods.service.GoodsHtmlService;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
//注入到容器
@Component
public class GoodsListener {
    @Autowired
    private GoodsHtmlService goodsHtmlService;
    //监听修改和保存，生成静态文件
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "LEYOU.ITEM.SAVE.QUEUE",durable = "true"),//配置队列
            exchange = @Exchange(value = "LEYOU.ITEM.EXCHANGE",ignoreDeclarationExceptions = "true",type = ExchangeTypes.TOPIC ),//配置交换机类型
            key = {"item.insert","item.update"}//routing key
    ))
    public void save(Long id){
        if(id==null){
            return;
        }
        goodsHtmlService.createHtml(id);
    }
    //监听删除，删除静态文件
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "LEYOU.ITEM.DELETE.QUEUE",durable = "true"),//配置队列
            exchange = @Exchange(value = "LEYOU.ITEM.EXCHANGE",ignoreDeclarationExceptions = "true",type = ExchangeTypes.TOPIC ),//配置交换机类型
            key = {"item.delete"}//routing key
    ))
    public void delete(Long id){
        if(id==null){
            return;
        }
        goodsHtmlService.deleteHtml(id);
    }
}
